<template>
  <div id="mapContent" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
require('echarts/extension/bmap/bmap')
import resize from '@/views/dashboard/components/mixins/resize'

export default {
  name: 'EffectScatterMap',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    title: {
      type: String,
      default: ''
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      default: function() {
        return []
      }
    },
    tooltipFormatter: {
      type: [String, Function],
      default: '{b}'
    },
    center: {
      type: Array,
      default: function() {
        return []
      }
    },
    zoom: {
      type: Number,
      default: 12
    }
  },
  data() {
    return {
      chart: null,
      bMap: null,
      timeout: null
    }
  },
  computed: {
    centerPoint() {
      if (this.center && this.center.length === 2) {
        return this.center
      } else if (this.chartData && this.chartData.length > 0) {
        const lng1 = Math.min(this.chartData.map(item => item.value[0]))
        const lng = Math.max(this.chartData.map(item => item.value[0]))
        const lat1 = Math.min(this.chartData.map(item => item.value[1]))
        const lat = Math.max(this.chartData.map(item => item.value[1]))
        return [(lng1 + lng) / 2, (lat1 + lat) / 2]
      } else {
        return [102.97, 18.71]
      }
    }
  },
  watch: {
    chartData: {
      handler(val) {
        this.setOptions()
        this.centerMap()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart() // 创建和初始化地图
      this.setOptions()
      this.initMap()
      setTimeout(this.centerMap, 100)
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
    },
    setOptions() {
      if (!this.chart) {
        return
      }
      this.chart.setOption({
        title: {
          text: this.title,
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: this.tooltipFormatter,
          textStyle: {
            color: '#fff'
          }
        },
        bmap: {
          zoom: 6,
          roam: 'move',
          mapOptions: {
            enableMapClick: false
          }
        },
        series: [{
          type: 'effectScatter',
          coordinateSystem: 'bmap',
          data: this.chartData,
          symbolSize: function(val) {
            return 12
          },
          encode: {
            value: 2
          },
          showEffectOn: 'render',
          rippleEffect: {
            brushType: 'stroke'
          },
          hoverAnimation: true,
          label: {
            formatter: '{b}',
            position: 'right',
            show: true
          },
          itemStyle: {
            shadowBlur: 10,
            shadowColor: '#333',
            color: '#f00'
          },
          zlevel: 1
        }]
      })
    },
    centerMap() {
      if (!this.chart) {
        this.initChart()
        this.setOptions()
      }
      if (!this.bMap) {
        this.initMap()
      }
      this.bMap.centerAndZoom(new BMap.Point(this.centerPoint[0], this.centerPoint[1]), this.zoom)
    },
    initMap() {
      this.bMap = this.chart.getModel().getComponent('bmap').getBMap()
    }
  }
}
</script>

<style>
div.anchorBL {
  display: none;
}
</style>
